﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.App_Code;
using QXBook.Utils;
using QXBook.Model;
using QXBook.Service;
using System.Configuration;
using System.Web.UI.HtmlControls;

namespace Web.SysManage.Article
{
    public partial class ArticleEdit : AdminBasePage
    {
        private void BindPart(ListControl selectControl)
        {
            selectControl.Items.Clear();
            selectControl.Items.Add(new ListItem("请选择", "0"));
            selectControl.Items.Add(new ListItem(ModelEnums.GetEnumDescription(Part.JRQXP), CommonUtils.ToString((int)Part.JRQXP)));
            selectControl.Items.Add(new ListItem(ModelEnums.GetEnumDescription(Part.SYYX), CommonUtils.ToString((int)Part.SYYX)));
            selectControl.Items.Add(new ListItem(ModelEnums.GetEnumDescription(Part.RSWW), CommonUtils.ToString((int)Part.RSWW)));
            selectControl.Items.Add(new ListItem(ModelEnums.GetEnumDescription(Part.QXPGY), CommonUtils.ToString((int)Part.QXPGY)));
            selectControl.Items.Add(new ListItem(ModelEnums.GetEnumDescription(Part.SYGK), CommonUtils.ToString((int)Part.SYGK)));
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindPart(ddlPart);
                ServiceFactory.CreateInstance<CategoriesService>().BindCategory(selCategory, Part.ZXFL, 0);
                selCategory.Items.Insert(0, new ListItem("请选择", "0"));

                if (!string.IsNullOrEmpty(CommonUtils.Get("ArticleID")))
                {
                    Title = "修改资讯内容";
                    hArticleID.Value = CommonUtils.Get("ArticleID");
                    ArticleInfo article = ServiceFactory.CreateInstance<ArticleService>().GetModel(hArticleID.Value);
                    this.DataBind(article);
                }
                else
                {
                    Title = "添加资讯内容";
                }
            }

            ddlPart.SelectedIndexChanged += new EventHandler(ddlPart_SelectedIndexChanged);
            btnSave.Click += new EventHandler(btnSave_Click);
        }

        void ddlPart_SelectedIndexChanged(object sender, EventArgs e)
        {
            Part partType = (Part)CommonUtils.ToInt(ddlPart.SelectedValue);
            selCategory.Items.Clear();
            ServiceFactory.CreateInstance<CategoriesService>().BindCategory(selCategory, partType, 0);
            selCategory.Items.Insert(0, new ListItem("请选择", "0"));
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void DataBind(IModelBase model)
        {
            ArticleInfo article = model as ArticleInfo;
            iTitle.Value = article.Title;
            ddlPart.SelectedValue = ((int)article.PartMod).ToString();
            selCategory.Value = article.CategoryID.ToString();
            iAuthor.Value = article.Author;
            iKeyWords.Value = article.KeyWord;
            taShortContent.Value = article.ShortContent;
            taContent.Value = article.Content;
            imgSmall.ImageUrl = article.SmallImg;
            imgNomarl.ImageUrl = article.BigImg;
            if (string.IsNullOrEmpty(article.SmallImg) && string.IsNullOrEmpty(article.BigImg))
            {
                phIMG.Visible = false;
            }
            else
            {
                phIMG.Visible = true;
            }
        }

        private ArticleInfo GetModel(ref ArticleInfo model)
        {
            string title = CommonUtils.Post("iTitle");
            string author = CommonUtils.Post("iAuthor");
            Part partMod = (Part)CommonUtils.ToInt(CommonUtils.Post("selPart"));
            int categoryID = CommonUtils.ToInt(CommonUtils.Post("selCategory"));
            string keywords = CommonUtils.Post("iKeyWords");
            string smallimg = string.Empty;
            string bigimg = string.Empty;
            HttpFileCollection files = Request.Files;
            if (files.Count > 0 && files[0].ContentLength > 0)
            {
                HttpPostedFile file = files[0];
                string normalPath = ConfigurationManager.AppSettings["ImgNormal"];   //普通图片路径
                string smallPath = ConfigurationManager.AppSettings["ImgSmall"];        //缩略图片路径
                FileUploadHelper fileUpload = new FileUploadHelper();
                fileUpload.PostedFile = file;
                fileUpload.SetFile(); //设置文件信息
                //保存缩略图到small中
                fileUpload.FileCompressPath = smallPath;
                //fileUpload.CompressImage(218, 203, "Cut");
                fileUpload.ReduceImage(0.5F);
                //保存图片到normal中
                fileUpload.FilePath = normalPath;
                fileUpload.SaveFile();
                smallimg = fileUpload.RefSmallFilePath;
                bigimg = fileUpload.RefNormalFilePath;
            }
            string shortContent = CommonUtils.Post("taShortContent");
            string content = CommonUtils.Post("taContent");

            if (string.IsNullOrEmpty(title))
            {
                //base.AjaxErch("{state:false,message:'用户名和昵称不能为空!'}");
                base.ResponseErch("资讯标题不能为空!");
                return null;
            }

            if (string.IsNullOrEmpty(hArticleID.Value))
            {
                model.IsPublish = YesNo.Yes;//添加时候默认发布
            }

            model.Title = title;
            model.PartMod = partMod;
            model.CategoryID = categoryID;
            model.Author = author;
            model.KeyWord = keywords;
            model.SmallImg = smallimg;
            model.BigImg = bigimg;
            model.ShortContent = shortContent;
            model.Content = content;
            return model;
        }

        private void Save()
        {
            string rtmessage = string.Empty;
            string resultMsg = string.Empty;
            try
            {
                ArticleInfo article = string.IsNullOrEmpty(hArticleID.Value) ? new ArticleInfo() : ServiceFactory.CreateInstance<ArticleService>().GetModel(hArticleID.Value);
                article = GetModel(ref article);

                if (!string.IsNullOrEmpty(hArticleID.Value))
                {
                    rtmessage = ServiceFactory.CreateInstance<ArticleService>().UpdateArticle(base.adminInfo.AdminName, article);
                    resultMsg = "资讯信息修改成功!";
                }
                else
                {
                    rtmessage = ServiceFactory.CreateInstance<ArticleService>().AddArticle(base.adminInfo.AdminName, article);
                    resultMsg = "成功添加新的资讯!";
                }
            }
            catch (Exception ex)
            {
                base.AjaxErch("{state:false,message:\"" + CommonUtils.JSEncode(ex.Message.ToString()) + "\"}");
                return;
            }

            base.ResponseErch(rtmessage, resultMsg);
        }
    }
}